﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using System.IO;
using Project_2._2.Extra;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;

namespace Project_2._2.Controllers
{
    public class AdminServicesController : Controller
    {
        //
        // GET: /DichVuThueXe/

        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        private Common cm = new Common();
        public ActionResult Default(int? page, string rd, string Record)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    if (rd != null)
                    {
                        int record = int.Parse(rd);
                        var dv = (from p in db.DichVuThueXes where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                        ViewBag.record = rd;
                        return View(dv);
                    }
                    else
                    {
                        var dv = (from p in db.DichVuThueXes where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, 3);
                        return View(dv);
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
        }
        [HttpPost]
        public ActionResult Default(FormCollection col, int? page)
        {
            int record = 0;
            if (string.IsNullOrEmpty(col["KeyWord"]))
            {
                record = int.Parse(col["Record"]);
                var dv = (from p in db.DichVuThueXes where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                ViewBag.record = col["Record"];
                return View(dv);
            }
            else
            {
                string SearchBy = col["SearchBy"].ToString();
                string key = col["KeyWord"].ToString();

                switch (SearchBy)
                {
                    case "TenDichVu":
                        var dv = (from p in db.DichVuThueXes where p.isDelete == false & p.TenDichVu.Contains(key) select p).ToList().ToPagedList(page ?? 1, 1);
                        ViewBag.record = "1";
                        return View(dv); break;
                }
                return View();
            }

        }

        [HttpPost]
        public ActionResult Multi_Delete(IEnumerable<int> multi_delete_dichvu)
        {
            var dv_delete = db.DichVuThueXes.Where(x => multi_delete_dichvu.Contains(x.DichVuId)).ToList();
            foreach (var dv in dv_delete)
            {
                dv.isDelete = true;
                UpdateModel(dv);
                db.SaveChanges();
            }
            return RedirectToAction("Default", "AdminServices");
        }
        public ActionResult Create()
        {
            try
            {
                if (!Request.IsAuthenticated)
                {
                    return RedirectToAction("Login", "Admin");
                }
                else
                {
                    if (cm.CheckUserType(User.Identity.Name))
                    {
                        // var th = (from p in db.ThuongHieux where p.ThuongHieuId == id select p).First();
                        return View();
                    }
                    else
                        return RedirectToAction("Login", "Admin");
                }
            }
            catch
            {
                return RedirectToAction("SystemErrors", "Default");
            }
        }
        [HttpPost]
        public ActionResult Create(DichVuThueXe_ViewMdels dv)
        {
            if (ModelState.IsValid)
            {
                if (dv.TenDichVu == null)
                {
                    ModelState.AddModelError("", "Tên dịch vụ không được để trống...!");
                }
                else
                {
                    DichVuThueXe dichvu = new DichVuThueXe();
                    dichvu.TenDichVu = dv.TenDichVu;
                    dichvu.MoTa = dv.MoTa;
                    dichvu.isDelete = false;
                    db.DichVuThueXes.Add(dichvu);
                    db.SaveChanges();
                    ModelState.Clear();
                    dv = new DichVuThueXe_ViewMdels();
                    ModelState.AddModelError("", "Đã thêm thành công...!");
                }
            }
            return View(dv);
        }
        public ActionResult Delete(int id)
        {
            DichVuThueXe dichvu = (from p in db.DichVuThueXes where p.DichVuId == id select p).First();
            dichvu.isDelete = true;
            UpdateModel(dichvu);
            db.SaveChanges();
            return RedirectToAction("Default", "AdminServices");
        }
        public ActionResult Update(int id)
        {
            try
            {
                if (!Request.IsAuthenticated)
                {
                    return RedirectToAction("Login", "Admin");
                }
                else
                {
                    if (cm.CheckUserType(User.Identity.Name))
                    {
                        var dv = (from p in db.DichVuThueXes where p.DichVuId == id select p).First();
                        return View(dv);
                    }
                    else
                        return RedirectToAction("Login", "Admin");
                }
            }
            catch
            {
                return RedirectToAction("SystemErrors", "Default");
            }
            
        }
        [HttpPost]
        public ActionResult Update(int id, FormCollection col)
        {
            var dv = (from p in db.DichVuThueXes where p.DichVuId == id select p).First();
            dv.TenDichVu = col["TenDichVu"];
            dv.MoTa = col["MoTa"];
            UpdateModel(dv);
            db.SaveChanges();
            ModelState.AddModelError("", "Cập nhật thành công.");
            return RedirectToAction("Default");
        }
    }
}
